<template>
    <!-- 表格组件 -->
    <el-table :data="tableData" border>
        <el-table-column width="200" label="日期">
            <template #default="scope">
                <div style="display: flex; align-items: center">
                    <el-icon>
                        <timer />
                    </el-icon>
                    <span style="margin-left: 10px">{{ scope.row.advert_create_time }}</span>
                </div>
            </template>
        </el-table-column>
        <el-table-column width="200" label="名称" prop="advert_title" />
        <el-table-column width="200" label="描述" prop="advert_text" />
        <el-table-column width="80" label="链接">
            <template #default="scope">
                <a :href="scope.row.advert_href" target="_blank">链接</a>
            </template>
        </el-table-column>
        <el-table-column label="图片">
            <template #default="scope">
                <span>{{ scope.row.advert_src }}</span>
            </template>
        </el-table-column>
        <el-table-column>
            <template #header>操作</template>
            <template #default="scope">
                <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
                <el-button size="small" @click="handleDelete(scope.$index, scope.row)" type="danger">删除</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>

<script lang="ts" setup>
// 导入消息提示框
import { ElMessage } from 'element-plus';
import { reactive } from 'vue';
import { advertListApi, advertRemoveApi } from '../../http/request';
import moment from "moment";
// 导入创建路由方法
import { useRouter } from 'vue-router';
// 定义路由
const $router = useRouter();
// 用户临时身份token
let token = localStorage.getItem('token');
// 定义数据结构
interface Advert {
    advert_create_time: string
    advert_title: string
    advert_href: string
    advert_text: string
    advert_src: string
    advert_id: string
}
// 记录数据
let tableData: Advert[] = reactive([]);
// 交互行为1
const handleEdit = (index: number, row: Advert) => {
    // console.log('edit',index, row)
    $router.push({ path: '/advert/publish', query: { advert_id: row.advert_id } })
}
// 交互行为2
const handleDelete = (index: number, row: Advert) => {
    // 获取广告id
    let advert_id = row.advert_id;
    // console.log("图片ID:",advert_id);
    if (confirm('您确定删除该广告吗')) {
        // 删除广告和图片
        advertRemoveApi({ token, advert_id })
            .then(
                (data: any) => {
                    if (data.code == 200) {
                        tableData.splice(index, 1)
                    }
                    // 提示
                    ElMessage({
                        message: data.msg,
                        type: 'warning',
                    });
                }
            )
    }
}
// 初始化广告列表
const initAdvertList = async () => {
    // 调用广告列表接口
    await advertListApi()
        .then(
            (data: any) => {
                if (data.code == 200) {
                    // console.log(data);
                    let { result } = data;
                    result.map((item: any) => {
                        if (item.advert_create_time) {
                            item.advert_create_time = moment(new Date(item.advert_create_time)).format('YYYY-MM-DD hh:mm:ss')
                        }
                        tableData.push(item)
                    });
                }
            }
        )
}
// 初始化 
initAdvertList();
</script>